package org.springblade.core.secure.auth;

import org.springblade.core.secure.utils.SecureUtil;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 权限判断
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2021/3/12 17:22
 */
public class AuthorizeFun {

	/**
	 * 判断是否有该按钮权限
	 *
	 * @param permission 权限标识
	 * @return {boolean}
	 */
	public boolean hasPermission(String permission) {
		return hasAnyPermission(permission);
	}

	/**
	 * 判断接口是否有该按钮权限
	 *
	 * @param permissions 权限标识集合
	 * @return {boolean}
	 */
	public boolean hasAnyPermission(String... permissions) {
		List<String> permissionList = SecureUtil.getUserPermissions();
		if (null == permissionList) {
			return false;
		}
		return permissionList.stream().filter(StringUtils::hasText)
			.anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
	}
}
